<?php

$mobile = request('mobile');
$code = request('code');
if (!$mobile || !$code) {
    $msg = array("ip" => "127.0.0.2", "errcode" => "1", "errmsg" => "mobile or code invalid");
    $sout = json_encode($msg);
    echo utf8_encode($sout);
    exit;
}
$result = dbquery($mobile, $code);
if ($result) {
    $msg = array("ip" => "127.0.0.2", "errcode" => "0", "errmsg" => "ok");
} else {
    $msg = array("ip" => "127.0.0.2", "errcode" => "1", "errmsg" => "error");
}
$sout = json_encode($msg);
echo utf8_encode($sout);
exit;

// ======================functions==============================
function request($name, $maxlen = 32) {
    $value = '';
    if (isset($_REQUEST[$name])) {
        $value = $_REQUEST[$name];
        if ($value) {
            $value = htmlentities($value);
            if (strlen($value) > $maxlen) {
                $value = '';
            }
        }
    }
    return $value;
}

function dbquery($mobile, $code) {
    $link = mysqli_connect('106.15.49.79', 'root', 'xyx5659506bT!@#123', 'nodejs');
    mysqli_select_db($link, 'nodejs');
    mysqli_set_charset($link, 'utf8');
    $now=date('Y-m-d H:i:s');
    $sqlstmt = "select count(0) as count from t_sms_captcha where mobile='$mobile' and captcha='$code' and expire_time >= '$now'";
    $result = mysqli_query($link, $sqlstmt);
    $row = mysqli_fetch_assoc($result);
    $result = $row['count'];
    mysqli_close($link);
    return $result;
}


/*
 * cre
 */